package com.ruoyi.api.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vim.domain.ImServiceAllocationObject;
import com.ruoyi.vim.service.IImServiceAllocationObjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客服与分配对象关联Controller
 *
 * @author wgp
 * @since 2022-12-14
 */
@RestController
@RequestMapping("/vim/object")
public class ImServiceAllocationObjectController extends BaseController<ImServiceAllocationObject> {
    @Autowired
    private IImServiceAllocationObjectService imServiceAllocationObjectService;

    /**
     * 查询客服与分配对象关联列表
     */
    @PreAuthorize("@ss.hasPermi('vim:object:list')")
    @GetMapping("/list")

    /**
     * 列表
     * @param imServiceAllocationObject 实体
     * @param pageNum 页码
     * @param pageSize 每页多少条
     */
    public TableDataInfo list(ImServiceAllocationObject imServiceAllocationObject, Long pageNum, Long pageSize) {
        QueryWrapper<ImServiceAllocationObject> queryWrapper = getQueryWrapper(imServiceAllocationObject);
        IPage<ImServiceAllocationObject> iPage = imServiceAllocationObjectService.page(new Page<ImServiceAllocationObject>(pageNum, pageSize), queryWrapper);
        return getDataTable(iPage);
    }

    /**
     * 导出客服与分配对象关联列表
     */
    @PreAuthorize("@ss.hasPermi('vim:object:export')")
    @Log(title = "客服与分配对象关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImServiceAllocationObject imServiceAllocationObject) {
        QueryWrapper<ImServiceAllocationObject> queryWrapper = getQueryWrapper(imServiceAllocationObject);
        List<ImServiceAllocationObject> list = imServiceAllocationObjectService.list(queryWrapper);
        ExcelUtil<ImServiceAllocationObject> util = new ExcelUtil<>(ImServiceAllocationObject.class);
        util.exportExcel(response, list, "客服与分配对象关联数据");
    }

    /**
     * 获取客服与分配对象关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('vim:object:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(imServiceAllocationObjectService.getById(id));
    }

    /**
     * 新增客服与分配对象关联
     */
    @PreAuthorize("@ss.hasPermi('vim:object:add')")
    @Log(title = "客服与分配对象关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImServiceAllocationObject imServiceAllocationObject) {
        return toAjax(imServiceAllocationObjectService.save(imServiceAllocationObject));
    }

    /**
     * 修改客服与分配对象关联
     */
    @PreAuthorize("@ss.hasPermi('vim:object:edit')")
    @Log(title = "客服与分配对象关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImServiceAllocationObject imServiceAllocationObject) {
        return toAjax(imServiceAllocationObjectService.updateById(imServiceAllocationObject));
    }

    /**
     * 删除客服与分配对象关联
     */
    @PreAuthorize("@ss.hasPermi('vim:object:remove')")
    @Log(title = "客服与分配对象关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(imServiceAllocationObjectService.removeBatchByIds(Arrays.asList(ids)));
    }

    private QueryWrapper<ImServiceAllocationObject> getQueryWrapper(ImServiceAllocationObject imServiceAllocationObject) {
        QueryWrapper<ImServiceAllocationObject> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(imServiceAllocationObject.getServiceId())) {
            queryWrapper.eq("service_id", imServiceAllocationObject.getServiceId());
        }
        if (StringUtils.isNotEmpty(imServiceAllocationObject.getAllocationObjectId())) {
            queryWrapper.eq("allocation_object_id", imServiceAllocationObject.getAllocationObjectId());
        }
        return queryWrapper;
    }
}
